package com.zx.map.service.impl;

import com.zx.map.dao.BaseAcctInfoDao;
import com.zx.map.dao.model.BaseAcctInfo;
import com.zx.map.dao.model.BaseAcctInfoExample;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.MerchantInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("baseAcctInfoService")
public class BaseAcctInfoServiceImpl implements BaseAcctInfoService {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(BaseAcctInfoServiceImpl.class);

    @Autowired
    private BaseAcctInfoDao dao;

    @Autowired
    private MerchantInfoService merchantInfoService;

    /**
     * 得到所有数据列表
     */
    public List<BaseAcctInfoModel> getAllList() throws DataAccessException {

        List<BaseAcctInfo> list = dao.getListByExample(null);
        List<BaseAcctInfoModel> modelList = new ArrayList<BaseAcctInfoModel>();
        for (BaseAcctInfo o : list) {
            BaseAcctInfoModel temp = new BaseAcctInfoModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;

    }

    /**
     * 根据单Key查找Model
     */
    public BaseAcctInfoModel getModelByKey(String key) throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(key));
        BaseAcctInfo o = dao.selectByPrimaryKey(key);
        BaseAcctInfoModel model = new BaseAcctInfoModel();
        BeanUtils.copyProperties(o, model);
        return model;
    }

    /**
     * 根据单Key查找Model  同时增加行锁
     */
    public BaseAcctInfoModel getModelByKeyAndLineLock(String key) throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(key));
        BaseAcctInfo o = dao.selectByPrimaryKeyAndLineLock(key);
        BaseAcctInfoModel model = new BaseAcctInfoModel();
        BeanUtils.copyProperties(o, model);
        return model;
    }

    /**
     * 根据Model查找列表
     */
    public List<BaseAcctInfoModel> getListByModel(BaseAcctInfoModel model) throws DataAccessException {
        BaseAcctInfoExample example = new BaseAcctInfoExample();
        com.zx.map.dao.model.BaseAcctInfoExample.Criteria criteria = example.createCriteria();
        /*if (StringUtils.isNotBlank(model.getCompanyName())) {
            criteria.andCompanyNameEqualTo(model.getCompanyName());
		}*/
        List<BaseAcctInfo> list = dao.getListByExample(example);
        List<BaseAcctInfoModel> modelList = new ArrayList<BaseAcctInfoModel>();
        for (BaseAcctInfo o : list) {
            BaseAcctInfoModel temp = new BaseAcctInfoModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public String add(BaseAcctInfoModel model) throws DataAccessException {
        BaseAcctInfo temp = new BaseAcctInfo();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(BaseAcctInfoModel model) throws DataAccessException {
        BaseAcctInfo temp = new BaseAcctInfo();
        BeanUtils.copyProperties(model, temp);
        dao.update(temp);
        return model.getCustId();
    }

    /**
     * 删除
     */
    public void delete(BaseAcctInfoModel model) throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(model.getCustId()));
        BaseAcctInfo temp = new BaseAcctInfo();
        BeanUtils.copyProperties(model, temp);
        dao.delete(temp);
    }

    /**
     * 分页查询
     *
     * @param pageSize
     * @param pageNum
     * @param domain
     * @return
     * @throws DataAccessException
     */
    public Map<String, Object> getPageByDomain(int pageSize, int pageNum, BaseAcctInfoModel domain)
            throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        BaseAcctInfoExample example = new BaseAcctInfoExample();
        com.zx.map.dao.model.BaseAcctInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(domain.getCustId())) {
            criteria.andCustIdEqualTo(domain.getCustId());
        }
        //排序add by wenjw 2015.6.2
        example.setOrderByClause("create_Date desc");
        int pageBegin = (pageNum - 1) * pageSize;
        List<BaseAcctInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
        List<BaseAcctInfoModel> menuModelList = new ArrayList<BaseAcctInfoModel>();
        for (BaseAcctInfo o : list) {
            BaseAcctInfoModel temp = new BaseAcctInfoModel();
            BeanUtils.copyProperties(o, temp);
            menuModelList.add(temp);
        }
        int count = dao.countByExample(example);
        map.put("count", count);
        map.put("list", menuModelList);
        return map;
    }

    @Override
    public Map<String, Object> queryPageByDomain(MerchantInfoModel searchMerchantInfoModel, BaseAcctInfoModel domain, int pageSize, int pageNum)
            throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        List<String> merchantIdList=merchantInfoService.queryListMerchantIdByModel(searchMerchantInfoModel);

        BaseAcctInfoExample example = new BaseAcctInfoExample();
        BaseAcctInfoExample.Criteria criteria = example.createCriteria();
        if(merchantIdList.size()==0){
            merchantIdList.add(new String("-1"));
        }
        criteria.andCustIdIn(merchantIdList);
        example.setOrderByClause("create_Date desc");
        int pageBegin = (pageNum - 1) * pageSize;
        List<BaseAcctInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
        List<BaseAcctInfoModel> menuModelList = new ArrayList<BaseAcctInfoModel>();
        for (BaseAcctInfo o : list) {
            BaseAcctInfoModel temp = new BaseAcctInfoModel();
            BeanUtils.copyProperties(o, temp);
            MerchantInfoModel merchantInfoModel = merchantInfoService.getModelByKey(o.getCustId());
            temp.setMerchantName(merchantInfoModel.getMerchantName());
            temp.setSysAccount(merchantInfoModel.getSysAccount());
            menuModelList.add(temp);
        }
        double totalAvlbal = dao.countAvlbal(example);
        double totalFrzbal = dao.countFrzbal(example);
        int count = dao.countByExample(example);
        map.put("totalAvlbal",totalAvlbal);
        map.put("totalFrzbal",totalFrzbal);
        map.put("count", count);
        map.put("list", menuModelList);
        return map;
    }

    /**
     * 对于指定用户增加账户余额
     */
    @Override
    public BaseAcctInfoModel addBaseAcctBalByKeyModel(BigDecimal settleAmt, BaseAcctInfoModel model)
            throws DataAccessException {
        Assert.isTrue(null != model);
        if (null != model) {
            // 加入可用余额
            model.setAcctBal(new BigDecimal(model
                    .getAcctBal().toString()).add(settleAmt)
                    .doubleValue());
            model.setAvlBal(new BigDecimal(model
                    .getAvlBal().toString()).add(settleAmt)
                    .doubleValue());
            update(model);
        } else {
            log.error("系统内部异常 未发现用户账户信息");
            throw new RuntimeException("系统内部异常 未发现用户账户信息");
        }
        return model;
    }

    /**
     * 对于指定用户冻结账户余额
     */
    @Override
    public BaseAcctInfoModel frzBaseAcctBalByKey(BigDecimal settleAmt, String key)
            throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(key));
        BaseAcctInfoModel tempBaseAcct = getModelByKey(key);
        if (null != tempBaseAcct) {
            // 冻结指定金额
            tempBaseAcct.setAvlBal(new BigDecimal(tempBaseAcct
                    .getAvlBal().toString()).subtract(settleAmt)
                    .doubleValue());
            tempBaseAcct.setFrzBal(new BigDecimal(tempBaseAcct.getFrzBal().toString()).add(settleAmt)
                    .doubleValue());
            update(tempBaseAcct);
        } else {
            log.error("系统内部异常 未发现用户账户信息");
            throw new RuntimeException("系统内部异常 未发现用户账户信息");
        }
        return tempBaseAcct;
    }

    /**
     * 对于指定用户解冻账户余额
     */
    @Override
    public BaseAcctInfoModel deFrzBaseAcctBalByKey(BigDecimal settleAmt, String key)
            throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(key));
        BaseAcctInfoModel tempBaseAcct = getModelByKey(key);
        if (null != tempBaseAcct) {
            // 冻结指定金额
            tempBaseAcct.setAvlBal(new BigDecimal(tempBaseAcct
                    .getAvlBal().toString()).add(settleAmt)
                    .doubleValue());
            tempBaseAcct.setFrzBal(new BigDecimal(tempBaseAcct.getFrzBal().toString()).subtract(settleAmt)
                    .doubleValue());
            update(tempBaseAcct);
        } else {
            log.error("系统内部异常 未发现用户账户信息");
            throw new RuntimeException("系统内部异常 未发现用户账户信息");
        }
        return tempBaseAcct;
    }
}
